/*
 * Copyright (c) 2005 -2008 Reliable Source, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Mozilla Public License v.1.1,
 * BSD, and Eclipse Public License v1.0 which accompanies this
 * distribution and is available at:
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 * 		http://www.opensource.org/licenses/bsd-license.php
 * 		http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * Contributors:
 *     IBM Corporation	     -  http://ibm.com
 *     	- initial API and implementation
 *
 *     patientos.org         - http://patientos.org
 *     	- domain and database persistence layer
 *
 *     Reliable Source, Inc. - http://reliablesource.org
 *     	- Symbiosys Application Framework,
 *     	- Symbiosys Application, Symbiosys Product
 *
 */
package org.reliablesource.client.ui.symbiosys.platform;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Hashtable;
import java.util.Iterator;

import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.core.runtime.IAdapterManager;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.TreeNode;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import org.reliablesource.model.core.ReliableNode;


/**
 *
 * SymbiosysPlatformActivator.java<br>
 * Created on: Jun 27, 2008<br>
 * <p>
 * The activator class controls the plug-in life cycle
 * </p>
 *
 * @author <a href="mailto:dennis.park@gmail.com">Dennis Park</a>
 *
 * @category
 * @since 1.0
 */
public class SymbiosysPlatformActivator extends AbstractUIPlugin {
    // The plug-in ID
    public static final String PLUGIN_ID = "org.reliablesource.client.ui.symbiosys.platform";

    // The shared instance
    private static SymbiosysPlatformActivator plugin;

    /**
     * Key to the image descriptor will be the image path
     */
    public Hashtable<String, ImageDescriptor> imgTable;
    private IAdapterFactory adapterFactory;
    private IAdapterManager adapterManager;

    /**
     * The constructor
     */
    public SymbiosysPlatformActivator() {
        super();
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
     */
    public void start(BundleContext context) throws Exception {
        super.start(context);
        plugin = this;

        adapterManager = Platform.getAdapterManager();
        adapterFactory = SymbiosysElementAdapterFactory.getInstance();
        adapterManager.registerAdapters(adapterFactory, ReliableNode.class);
        adapterManager.registerAdapters(adapterFactory, TreeNode.class);

        // compositeContextMap = new HashMap<String, Widget>();
        imgTable = new Hashtable<String, ImageDescriptor>();

        // LoginController.authenticateUser(new LoginModel("DEMO", "demo"));

        // PatientListController _patientListController =
        // PatientListController.getInstance(ISApplicationMediator.getInstance(),ApplicationController.getInstance()
        // );
        //              
        // List<ViewPatientModel> patients =
        // PatientService.getPatientsByPatientList(State.getUserRefId(),
        // ContextReference.PATIENTLISTMYPATIENTS.getRefId());
    }

    public void stop(BundleContext context) throws Exception {
	adapterManager.unregisterAdapters(adapterFactory, ReliableNode.class);
	adapterManager.unregisterAdapters(adapterFactory, TreeNode.class);
        plugin = null;
        super.stop(context);
    }

    /**
     * Returns the shared instance
     *
     * @return the shared instance
     */
    public static SymbiosysPlatformActivator getDefault() {
        return plugin;
    }

    /**
     * Returns an image descriptor for the image file at the given plug-in
     * relative path
     *
     * @param path
     *                the path
     * @return the image descriptor
     */
    public static ImageDescriptor getImageDescriptor(String path) {
        if (getDefault().imgTable.containsKey(path))
            return getDefault().imgTable.get(path);
        return imageDescriptorFromPlugin(PLUGIN_ID, path);
    }

    /**
     *
     * @return <code>IAdapterFactory</code> instance
     */
    public IAdapterFactory getElementFactory() {
        return adapterFactory;
    }
    
//    public static ImageDescriptor getImageDescriptor(String name) {
//	       String iconPath = "icons/";
//	       try {
//	           URL installURL = getDefault().getDescriptor().getInstallURL();
//	           URL url = new URL(installURL, iconPath + name);
//	           return ImageDescriptor.createFromURL(url);
//	       } catch (MalformedURLException e) {
//	           // should not happen
//	           return ImageDescriptor.getMissingImageDescriptor();
//	       }
//	    }
//
//	    public void dispose() {
//	       for (Iterator i = imageCache.values().iterator(); i.hasNext();) {
//	           ((Image) i.next()).dispose();
//	       }
//	       imageCache.clear();
//	    }
    
}
